package ch.unisi.inf.pfii.teamwhite.risk.core.player;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch
 *
 */

import java.util.ArrayList;

import ch.unisi.inf.pfii.teamwhite.risk.core.Planet;

//This abstract class for a player
public abstract class Player {
	
	private String name;
	private String type;
	private String color;
	
	private ArrayList<Planet> territory;
	private int money;
	
	private boolean duringAssignment;

	public Player(final String name, final String type) {
		this.name = name;
		duringAssignment = true;
		money = 2000;
		territory = new ArrayList<Planet>();
		this.setType(type);
	}
	
	public abstract void play(final ArrayList<Planet> planets, final String name, final int number);
	
	public abstract void assignPlanets(final ArrayList<Planet> planets);

//	Getters and setters
	public final String getName() {
		return name;
	}

	public final void setName(final String name) {
		this.name = name;
	}

	public final String getType() {
		return type;
	}
	
	public final void setType(String type) {
		this.type = type;
	}
	
	public final String getColor() {
		return color;
	}
	
	public final void setColor(final String color) {
		this.color = color;
	}
	
	public final ArrayList<Planet> getTerritory() {
		return territory;
	}
	
	public final void setTerritory(final ArrayList<Planet> territory) {
		this.territory = territory;
	}

	public final int getMoney() {
		return money;
	}

	public final void setMoney(final int money) {
		this.money = money;
	}
	
	public final boolean isDuringAssignment() {
		return duringAssignment;
	}
	
	public final void setDuringAssignment(boolean douringAssignment) {
		this.duringAssignment = douringAssignment;
	}

//	This method add a planet to the territories of the the player and changes its owner
	public void conquerPlanet(final Planet planet) {
	}
	
//	This method return some fields of the player.
	public final String getPlayerInfo() {
		return ("The player " + getName() + " has " + getMoney() + " credits");
	}

//  This method moves a certain amount of ships from a planet to a neighbour
	public final void moveShips(final Planet originPlanet, final Planet destinationPlanet, final int howMany) {
		if (originPlanet.getShips().size() > 0) {
			if (howMany < originPlanet.getShips().size() && howMany > 0) {
				for (int i = 0; i < howMany; i++) {
					destinationPlanet.createShip();
				}
				removeRange(howMany, originPlanet);
			}
		}
	}
	
//	This method remove the moved ships from the origin planet
	private final void removeRange(final int alreadyMoved, final Planet planet) {
		int index = planet.getShips().size();
		for (int i = 0; i < index - 1; i++) {
			planet.getShips().remove(0);
		}
	}
}
